//
//  DCIMUserInfoViewController.m
//  xmppBase
//
//  Created by striby on 16/1/26.
//  Copyright © 2016年 com.dc. All rights reserved.
//

#import "DCIMUserInfoViewController.h"
#import "DCSettingTableViewCell.h"
#import "DCMeInfoTableViewCell.h"
@interface DCIMUserInfoViewController ()

@end

@implementation DCIMUserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, DCDeviceWidth, DCDeviceHeight)];
    self.tableView.delegate=self;
    self.tableView.dataSource =self;
    self.tableView.backgroundColor=[UIColor groupTableViewBackgroundColor];
    
    
    [self.view addSubview:self.tableView];
    
    [self.tableView setContentInset:UIEdgeInsetsMake(0, 0, TabBarHeight, 0)];
    
    [self.tableView setShowsVerticalScrollIndicator:NO];
    
    self.tableView.separatorInset = UIEdgeInsetsZero;
    

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(friendShipChanged) name:kFriendShipStatusChanged object:nil];
    

    
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -tableView delegate
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 18;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 18;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, DCDeviceWidth, 18)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, DCDeviceWidth, 18)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==1) {
        if (indexPath.row==0) {
           
        }
        if (indexPath.row==2) {
            //            [self showLeadingView];
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        
        return 80;
    }
    return 40;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section==1) {
        return 3;
    }
    else
    {
        return 1;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  
    
    XMPPvCardTemp * card;
    
    if (self.userCard==nil) {
        card = [[DCXMPPTool shareInstance].xmppvCardTempModule vCardTempForJID:self.userJid shouldFetch:YES];
    }
    else
    {
        card = self.userCard;
    }
    
    
    
    if (indexPath.section==0)
    {
        DCMeInfoTableViewCell *cell = [[[NSBundle mainBundle]loadNibNamed:@"DCMeInfoTableViewCell" owner:nil options:nil]lastObject];

        
        
        cell.UserNickName.text = card.nickname;
        
        if (self.userJid) {
            cell.UserAccount.text =self.userJid.user;
        }
        
        
        if (!card.photo) {
            cell.UserIcon.image = [UIImage imageNamed:@"defaultUserIcon"];
        }
        else
        {
            cell.UserIcon.image=[UIImage imageWithData:card.photo];
        }
        
        
        cell.UserIcon.userInteractionEnabled = YES;
        
       

        
        return cell;
    }
    else if(indexPath.section==1)
    {
        
        DCSettingTableViewCell * cell = [[[NSBundle mainBundle]loadNibNamed:@"DCSettingTableViewCell" owner:nil options:nil]lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        if (indexPath.row==0) {
            cell.CellTitle.text = @"手机号码";
             cell.CellDetail.text = [[card elementForName:@"tel"] stringValue];
        }
        else if(indexPath.row==1)
        {
 
            cell.CellTitle.text = @"邮箱地址";
            cell.CellDetail.text = [[card elementForName:@"email"] stringValue];
        }
        else
        {

            cell.CellTitle.text = @"性别";
            
            if ([[[card elementForName:@"gender"]stringValue] integerValue]==DCBoy) {
                 cell.CellDetail.text = @"男";
            }
            else if([[[card elementForName:@"gender"]stringValue] integerValue]==DCGirl)
            {
                cell.CellDetail.text = @"女";
            }
            else
            {
                cell.CellDetail.text = @"未知";
            }
           
        }
        
        return cell;
        
        
    }
    else
    {
        
        
        
        UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"btn"];
        cell.backgroundColor = [UIColor clearColor];
        cell.contentView.backgroundColor = [UIColor clearColor];
        
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(40, 0, DCDeviceWidth-80, cell.bounds.size.height)];
        
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        if(indexPath.section==2)
        {
            [btn setBackgroundImage:[UIImage imageNamed:@"register_button"] forState:UIControlStateNormal];
            [btn setTitle:@"发送消息" forState:UIControlStateNormal];
            
            [btn addTarget:self action:@selector(sendMessage) forControlEvents:UIControlEventTouchUpInside];
        }
        else
        {
            NSArray * arr = [[DCXMPPTool shareInstance].xmppRosterMemoryStorage unsortedUsers];
            
            
            BOOL isMyFriend = NO;
            
            for (XMPPUserMemoryStorageObject *user in arr) {
                if (self.userJid) {
                    
                    if ([user.jid.user isEqualToString:self.userJid.user]) {
                        isMyFriend=YES;
                        break;
                    }
                    
                }
                
                
            }
            
            
            if (isMyFriend) {
                [btn setBackgroundImage:[UIImage imageNamed:@"red_background"] forState:UIControlStateNormal];
                [btn setTitle:@"删除好友" forState:UIControlStateNormal];
                
                  [btn addTarget:self action:@selector(deleteFriend) forControlEvents:UIControlEventTouchUpInside];
            }
            else
            {
                [btn setBackgroundImage:[UIImage imageNamed:@"register_button"] forState:UIControlStateNormal];
                [btn setTitle:@"添加好友" forState:UIControlStateNormal];
                
                  [btn addTarget:self action:@selector(addFriend) forControlEvents:UIControlEventTouchUpInside];
            }

        }
        
        
        
        
        [cell.contentView addSubview:btn];
        
        return cell;
        
    }
    
    
}

#pragma mark - private method
-(void)addFriend
{
    if (self.userJid) {
        [[DCXMPPTool shareInstance] addFriendWithUserJid:self.userJid];
    }
    
     [self.navigationController setViewControllers:@[self.navigationController.viewControllers[0]]];
    
}

-(void)deleteFriend
{
    if (self.userJid) {
        [[DCXMPPTool shareInstance] deletaFriendWithUserJid:self.userJid];
    }
    
     [self.navigationController setViewControllers:@[self.navigationController.viewControllers[0]]];
}

-(void)sendMessage
{
    DCIMConversationViewController * vc = [[DCIMConversationViewController alloc]init];
    
    vc.targetJID =self.userJid;
    
    vc.conversationType = DCConversationTypePrivate;


    [self.navigationController pushViewController:vc animated:YES];

}


-(void)friendShipChanged
{
//    [self.navigationController setViewControllers:@[self.navigationController.viewControllers[0]]];
}
@end
